<?php

class Model {
    public $tableName;
    protected $has_many = null;
    protected $many_many = null;
    protected $belongs_to = null;
    protected $attributes = array();

    public function __construct($query = null) {
        if ($query != null)
            $this->loadFromQuery($query);
    }

    public function __set($key , $val) {
        if ($key != 'id')
            $this->attributes[$key] = $val;
    }

    public static function factory($modelName , $id = null) {
        $modelName = ucfirst($modelName) . 'Model';
        $model = new $modelName();
        $tableName = $model->tableName;
        if ($id != null) {
            $model->loadFromQuery("SELECT * FROM $tableName WHERE id = $id LIMIT 1;");
        }
        return $model;
    }

    public static function factoryQuery($modelName , $query) {
        $modelName = ucfirst($modelName) . 'Model';
        $model = new $modelName();
        $model->loadFromQuery($query);
        return $model;
    }

    public function loaded() {
        return $this->id != null;
    }

    public static function paginaze($collections , $page , $items = Pagination::ITEMS_PER_PAGE) {
        $retval = array();
        $start = ($page - 1) * $items;
        $end = $page * $items;
        foreach ($collections as $key => $collection) {
            if ($key >= $start && $key < $end)
                $retval[] = $collection;
        }

        return $retval;
    }

    public function delete() {
        if ($this->id != null) {
            mysql_query("DELETE FROM `" .$this->tableName. "` WHERE `id` = " . $this->id . ";");
        }
    }

    public function save() {
            if ($this->id == null) {
                    $attr = "`id`";
                    $values = "NULL";

                    foreach ($this->attributes as $key => $val) {
                            $attr .= ' , ';
                            $values .= ' , ';
                            $attr .= "`" . $key . "`";
                            $values .= "'" . $val . "'";
                    }

                    mysql_query("INSERT INTO " .$this->tableName. "($attr) VALUES($values);");
                    $result = mysql_query("SELECT max(id) AS id FROM " . $this->tableName . ";");
                    $this->attributes['id'] = mysql_insert_id();
            }
            else {
                    $data = "";

                    $i = 0;
                    foreach ($this->attributes as $key => $val) {
                            if ($i != 0) {
                                    $data .= ' , ';
                            }
                            $data .= "`" . $key . "` = ";
                            $data .= "'" . $val . "'";
                            $i++;
                    }

                    mysql_query("UPDATE " .$this->tableName. " SET $data WHERE id = " . $this->id . ";");
            }
    }

    public function __get($key) {
            //echo $key;
            //print_r($this->attributes);
            if ($this->has_many != null && in_array("hm_" . $key , $this->has_many)) {
                    $retval = array();
                    //foreach ($this->has_many as $key => $val) {
                            $modelName = ucfirst($key) . "Model";
                            $model = new $modelName();
                            $tableName = $model->tableName;
                            $rows = Model::getRowsFromQuery("SELECT * FROM $tableName WHERE ".$this->tableName."_id = ".$this->id.";");
                            foreach ($rows as $row) {
                                    $model = new $modelName();
                                    $model->setAttributes($row);
                                    $retval[] = $model;
                            }
                    //}
                    return $retval;
            }
            else if ($this->belongs_to != null && in_array("bm_" . $key , $this->belongs_to)) {
                    $modelName = ucfirst($key) . "Model";
                    $model = new $modelName();
                    $tableName = $model->tableName;
                    $fk_name = $tableName . "_id";
                    $model->loadFromQuery("SELECT * FROM $tableName WHERE id = ".$this->$fk_name.";");
                    return $model;
            }
            else if (array_key_exists($key , $this->attributes)) {
                    return $this->attributes[$key];
            }
            else
                    return null;

    }

    public function loadFromQuery($query) {
            $result = mysql_query($query);
            if ($result != null) {
                    while ($row = mysql_fetch_assoc($result)) {
                            break;
                    }
                    $this->setAttributes($row);
            }
    }

    public function setAttributes($row) {
            if ($row != null)
                    foreach ($row as $key => $value)
                            //$this->$key = $value;
                            $this->attributes[$key] = $value;
    }

    public static function getRowsFromQuery($query) {
        $retval = array();
        $result = mysql_query($query);
        while ($row = mysql_fetch_assoc($result))
                $retval[] = $row;
        return $retval;
    }
	
    public static function loadRowsFromQuery($query , $class) {
            $retval = array();
            $result = mysql_query($query);
            while ($row = mysql_fetch_assoc($result)) {
                    $model = new $class();
                    $model->setAttributes($row);
                    $retval[] = $model;
            }
            return $retval;
    }

    public function query($query) {
        mysql_query($query);
    }
}

?>